package com.walnut.servlet;

import com.walnut.dao.FoodDao;
import com.walnut.dao.OrderDao;
import com.walnut.dao.impl.FoodDaoImpl;
import com.walnut.dao.impl.OrderDaoImpl;
import com.walnut.pojo.Food;
import com.walnut.pojo.Order;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/foodList")
public class FoodServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = getServletContext();
        FoodDao foodDao = new FoodDaoImpl();
        OrderDao orderDao = new OrderDaoImpl();
        try {
            List<Food> foods = foodDao.select();
            List<Order> orders = orderDao.select();
            context.setAttribute("foods", foods);
            request.setAttribute("foods", foods);
            request.setAttribute("orders", orders);
            context.setAttribute("orders", orders);
            if (context.getAttribute("isAdmin").equals("1"))
                request.getRequestDispatcher("adminFoodList.jsp").forward(request, response);
            else request.getRequestDispatcher("userFoodList.jsp").forward(request, response);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
